/* $RCSFile: CrmRatingEdit.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/CrmRating/CrmRatingEdit.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:20  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:51  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Settings;
using AddonNice.Security;
using AddonNice.UI;
using AddonNice.Helpers;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.UI.WebControls;
using AddonNice.Settings.PageModules.CrmRating;

using AddonNice.CRMWrapper;
using AddonNice.CRMWrapper.CrmRating;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.CrmRating
{
    public partial class CrmRatingEdit : AddEditItemPage
    {
		
		protected AddonNice.UI.WebControls.IHtmlEditor DesktopText;

		RatingDB CrmRatingDB      =   new RatingDB();

        bool RatingExist
        {
            get
            {
                object ob   =   ViewState["RatingExist"];
                if ( ob == null )
                    return false;
                return (bool)ob;
            }
            set
            {
                ViewState["RatingExist"]    =   value;
            }
        }

        
        int ItemCount
        {
            get
            {
                object ob   =   ViewState["ItemCount"];
                if ( ob == null )
                    return 0;
                return (int)ob;
            }
            set
            {
                ViewState["ItemCount"]    =   value;
            }
        }

        string RatingEntityName
        {
            get
            {
                object ob   =   ViewState["RatingEntityName"];
                if ( ob == null )
                    return string.Empty;
                return (string)ob;
            }
            set
            {
                ViewState["RatingEntityName"]    =   value;
            }
        }

        Guid CrmRatingID 
        {
            get
            {
                object ob   =   ViewState["CrmRatingID"];
                if ( ob == null )
                    return Guid.Empty;
                return (Guid)ob;
            }
            set
            {
                ViewState["CrmRatingID"]    =   value;
            }
        }

        Guid CrmRatingCampaignID
        {
            get
            {
                object ob   =   ViewState["CrmRatingCampaignID"];
                if ( ob == null )
                    return Guid.Empty;
                return (Guid)ob;
            }
            set
            {
                ViewState["CrmRatingCampaignID"]    =   value;
            }
        }

        Guid CrmDetailID 
        {
            get
            {
                object ob   =   ViewState["CrmDetailID "];
                if ( ob == null )
                    return Guid.Empty;
                return (Guid)ob;
            }
            set
            {
                ViewState["CrmDetailID "]    =   value;
            }
        }

        Guid CrmDetailCampaignID
        {
            get
            {
                object ob   =   ViewState["CrmDetailCampaignID"];
                if ( ob == null )
                    return Guid.Empty;
                return (Guid)ob;
            }
            set
            {
                ViewState["CrmDetailCampaignID"]    =   value;
            }
        }
        
        Guid CrmEntityID
        {
            get
            {
                object ob   =   ViewState["CrmEntityID"];
                if ( ob == null )
                    return Guid.Empty;
                return (Guid)ob;
            }
            set
            {
                ViewState["CrmEntityID"]    =   value;
            }
        }
               
        double AverageRating
        {
            get
            {
                object ob   =   ViewState["AverageRating"];
                if ( ob == null )
                    return 0;
                return (double)ob;
            }
            set
            {
                ViewState["AverageRating"]    =   value;
            }
        }

        int TimesRated
        {
            get
            {
                object ob   =   ViewState["TimesRated"];
                if ( ob == null )
                    return 0;
                return (int)ob;
            }
            set
            {
                ViewState["TimesRated"]    =   value;
            }
        }

        
        /// <summary>
        /// Ctor
        /// </summary>
        public CrmRatingEdit() : base(true,true,true,true)
        {}

		/// <summary>
        /// The Page_Load event on this Page is used to obtain the ModuleID
        /// and ItemID of the CrmRating to edit.
        /// It then uses the RatingsDB() data component
        /// to populate the page's edit controls with the rating details.
        /// </summary>
        protected void Page_Load(object sender, System.EventArgs e) 
        {
			if (PageTraceSwitch.Sw.TraceVerbose || RatingTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load BEG isPostBack: {0}.",IsPostBack),(RatingTrace.Sw.TraceVerbose) ? RatingTrace.Sw.Info:PageTraceSwitch.Sw.Info);
            // If the page is being requested the first time, determine if an
            // CrmRating itemID value is specified, and if so populate page
            // contents with the CrmRating details
			DesktopText					                    =   GetEditor(PlaceHolderHTMLEditor);

			if (!IsPostBack) 
            {
                // First try to read the rating from DB/CRM
                CbEntityName.Items.Add(new RadComboBoxItem("None",string.Empty));
                CbEntityName.Items.Add(new RadComboBoxItem(CrmStr.account,CrmStr.account));
                CbEntityName.Items.Add(new RadComboBoxItem(CrmStr.contact,CrmStr.contact));
                CbEntityName.Items.Add(new RadComboBoxItem(CrmStr.lead,CrmStr.lead));
                CbEntityName.Items.Add(new RadComboBoxItem(CrmStr.product,CrmStr.product));
                CbEntityName.DataBind();
				SqlDataReader dr                            =   null;
			    RatingEntityName                            =   string.Empty;
				try
				{
					dr                                      =   CrmRatingDB.GetRatingGlobalized(ModuleID);

					// Load first row into DataReader
                    if ( dr.Read() )
                    {
                        TbTitleRating.Text                  =   (string)dr["Title"];
                        TbStarNumber.Value                  =   (int)dr["StarNumber"];
                        ItemCount                           =   (int)dr["ItemCount"];
                        object ob                           =   dr["CrmID"];
                        CrmRatingID                         =   ( ob == DBNull.Value ) ? Guid.Empty:(Guid)ob;
                        ob                                  =   dr["CrmCampaignID"];
                        CrmRatingCampaignID                 =   ( ob == DBNull.Value ) ? Guid.Empty:(Guid)ob;
                        RatingEntityName                    =   (string)dr["CrmEntityName"];
                        CreatedDate.Text                    =   ( (DateTime)dr["CreatedDate"] ).ToShortDateString();
                        RadComboBoxItem it                  =   CbEntityName.FindItemByValue(RatingEntityName);
                        if ( it != null )
                            it.Selected                     =   true;
                        RatingExist                         =   true;
		                if (RatingTrace.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load ItemCount: {0}.",ItemCount),RatingTrace.Sw.Info);
                        if ( ItemCount > 0 ) // don't change star if something already exists
                        {
                            TbStarNumber.Enabled            =   false;
                            CbEntityName.Enabled            =   false;
                        }
		                if (RatingTrace.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load CrmRatingCampaignID: {0}.",CrmRatingCampaignID),RatingTrace.Sw.Info);
                        // read the campaign
                        if ( CrmRatingCampaignID == Guid.Empty )
                            CrmRatingCampaignID             =   moduleSettingsDic[RStr.Campaign].ToGuid(); // take module 's default value
                        if ( CrmRatingCampaignID != Guid.Empty )
                        {
                            ADNDynCampaign dnc              =   DynEntity.GetFromCache<ADNDynCampaign>(CrmStr.campaign,Wrapper,ResetWrapper,CrmRatingCampaignID);
                            if ( dnc != null )
                            {
                                it                          =    new RadComboBoxItem(dnc.name,CrmRatingCampaignID.ToString());
                                it.Selected                 =   true;
                                CbRatingCampaign.Items.Add(it);
                                CbRatingCampaign.DataBind();
		                        if (RatingTrace.Sw.TraceVerbose)
                                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load Campaign: {0}.",dnc.name),RatingTrace.Sw.Info);
                            }
                        }
                        if ( !string.IsNullOrEmpty(RatingEntityName) )
                        {
                            CbAssocEntity.WebServiceEntity  =   (WSEntity)Enum.Parse(typeof(WSEntity),RatingEntityName,true);
                        }
                    }
                    else
                    {
		                if (RatingTrace.Sw.TraceVerbose)
                            Trace2.WriteLine("[V]CrmRatingsEdit Page_Load no Rating.",RatingTrace.Sw.Info);
                        TbStarNumber.Value                  =   moduleSettingsDic[RStr.NumberStars].ToInt();
                        CrmRatingCampaignID                 =   moduleSettingsDic[RStr.Campaign].ToGuid();
                            
                        if ( CrmRatingCampaignID != Guid.Empty )
                        {
                            ADNDynCampaign dnc              =   DynEntity.GetFromCache<ADNDynCampaign>(CrmStr.campaign,Wrapper,ResetWrapper,CrmRatingCampaignID);
                            if ( dnc != null )
                            {
                                RadComboBoxItem it          =    new RadComboBoxItem(dnc.name,CrmRatingCampaignID.ToString());
                                it.Selected                 =   true;
                                CbRatingCampaign.Items.Add(it);
                                CbRatingCampaign.DataBind();
		                        if (RatingTrace.Sw.TraceVerbose)
                                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load Campaign: {0}.",dnc.name),RatingTrace.Sw.Info);
                            }
                        }

                    }
				}
				catch (Exception ex)
				{
					if (RatingTrace.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CrmRatingsEdit Page_Load ex: {0}", ex),RatingTrace.Sw.Info);
					throw;
				}
				finally
				{
					if (dr != null)
						dr.Close();
				}
                if (RatingTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load isNewItem: {0}.",isNewItem),RatingTrace.Sw.Info);
                if ( !isNewItem ) 
                {
                    // Obtain a single row of CrmRating Detail information
					dr                                      =   null;
					try
					{
						dr                                  =   CrmRatingDB.GetSingleRatingDetailGlobalized(ItemID);

						// Load first row into DataReader
						if (dr.Read())
						{
							TbTitleRatingDetail.Text        =   (string)dr["Title"];
                            TbOrder.Value                   =   (int)dr["ItemOrder"];
							DesktopText.TextContent         =   (string)dr["Description"];
                            object ob                       =   dr["CrmID"];
                            CrmDetailID                     =   ( ob == DBNull.Value ) ? Guid.Empty:(Guid)ob;
                            ob                              =   dr["CrmCampaignID"];
                            CrmDetailCampaignID             =   ( ob == DBNull.Value ) ? Guid.Empty:(Guid)ob;
                            ob                              =   dr["CrmEntityID"];
                            CrmEntityID                     =   ( ob == DBNull.Value ) ? Guid.Empty:(Guid)ob;
                            AverageRating                   =   Math.Round((double)dr["AverageRating"],1);
                            LblAverageRated.Text            =   AverageRating.ToString();
                            TimesRated                      =   (int)dr["TimesRated"];
                            LblTimesRated.Text              =   TimesRated.ToString();
							}
                        if (RatingTrace.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load CrmDetailID: {0},RatingEntityName: {1}, TimesRated: {2}.",
                                            CrmDetailID,RatingEntityName,TimesRated),RatingTrace.Sw.Info);
                        // read the assoc crm entity and set it in combo
                        if ( CrmEntityID != Guid.Empty )
                        {
                            DynEntity dnrd      =   null;
                            switch(RatingEntityName)
                            {
                                case CrmStr.product:
                                dnrd            =   new ADNDynProduct(Wrapper,CrmEntityID);
                                break;
                                case CrmStr.account:
                                dnrd            =   new ADNDynAccount(Wrapper,CrmEntityID);
                                break;
                                case CrmStr.contact:
                                dnrd            =   new ADNDynLead(Wrapper,CrmEntityID);
                                break;
                                case CrmStr.lead:
                                dnrd            =   new ADNDynLead(Wrapper,CrmEntityID);
                                break;
                            }
                            if ( dnrd != null )
                            {
                                if ( dnrd.Retrieve() )
                                {
                                    RadComboBoxItem it  =   new RadComboBoxItem(dnrd.PrimaryFieldStringValue,CrmEntityID.ToString());
                                    it.Selected         =   true;
                                    CbAssocEntity.Items.Add(it);
                                    CbAssocEntity.DataBind();
                                    if (RatingTrace.Sw.TraceVerbose)
                                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load item: {0}.",it.Text),RatingTrace.Sw.Info);
                                }
                            }
                        }
                        
                        // read the campaign
                        if ( CrmDetailCampaignID  == Guid.Empty )
                            CrmDetailCampaignID              =   CrmRatingCampaignID; // take default rating value
                        if ( CrmDetailCampaignID  == Guid.Empty )
                            CrmDetailCampaignID              =   moduleSettingsDic[RStr.Campaign].ToGuid(); // take module 's default value
                        if ( CrmDetailCampaignID != Guid.Empty )
                        {
                            ADNDynCampaign dnc      =   DynEntity.GetFromCache<ADNDynCampaign>(CrmStr.campaign,Wrapper,ResetWrapper,CrmDetailCampaignID);
                            if ( dnc != null )
                            {
                                RadComboBoxItem it  =   new RadComboBoxItem(dnc.name,CrmDetailCampaignID.ToString());
                                it.Selected         =   true;
                                CbDetailCampaign.Items.Add(it);
                                CbDetailCampaign.DataBind();
                                if (RatingTrace.Sw.TraceVerbose)
                                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit Page_Load default campaign: {0}.",it.Text),RatingTrace.Sw.Info);
                            }
                        }
					}
					catch (Exception ex)
					{
						if (RatingTrace.Sw.TraceError)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CrmRatingsEdit Page_Load ex: {0}", ex),RatingTrace.Sw.Info);
						throw;
					}
					finally
					{
						if (dr != null)
							dr.Close();
					}
                    if ( RatingExist )
                    {
                        RadTabStrip1.SelectedIndex          =   1;
                        RadMultiPage1.SelectedIndex         =   1;
                        CbEntityName.Enabled                =   false;
                        CbRatingCampaign.Enabled            =   false;
                    }
                    if ( TimesRated > 0 )
                    {
                        CbAssocEntity.Enabled               =   false;
                        CbAssocEntity.Enabled               =   false;
                        CbDetailCampaign.Enabled            =   false;
                    }
                }
                else TbOrder.Value                          =   0;
            }
			if (PageTraceSwitch.Sw.TraceVerbose || RatingTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]CrmRatingsEdit Page_Load END.",(RatingTrace.Sw.TraceVerbose) ? RatingTrace.Sw.Info:PageTraceSwitch.Sw.Info);
        }

		/// <summary>
		/// Set the module guids with free access to this page
		/// </summary>
		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al = new ArrayList();
				al.Add ("893D0AF4-A50B-4657-8F28-456790B475F3"); // CrmRating
                al.Add ("CB230AD6-573E-487a-BC3E-C14B96EDBD7B"); // CrmRatingGrid
				return al;
			}
		}
		/// <summary>
        /// create or update an CrmRating.  It  uses the AddonNice.CrmRatingsDB()
        /// </summary>
        void CrmRatingsEdit_Update(object sender,AddonNice.UI.ADNPageEventArgs e)
        {
            bool useCrm                 =   moduleSettingsDic[RStr.UseCrm].ToBool();
            bool createItem             =   isNewItem; // store status now, after as we change itemid, this will no be Ok
            if (RatingTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update BEG useCrm: {0}, isNewItem: {1}, CrmRatingID: {2}.",
                    useCrm,isNewItem,CrmRatingID),RatingTrace.Sw.Info);
			try
			{
                ADNDynRating dnr        =   null;

                // Get the eventual associated CRM entity
                if ( CrmRatingID != Guid.Empty )
                {
                    dnr                 =   DynEntity.GetFromCache<ADNDynRating>(CrmStr.rating,Wrapper,ResetWrapper,CrmRatingID);
                }
                bool createRating       =   (dnr == null);
                if (RatingTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update createRating: {0}.",createRating),RatingTrace.Sw.Info);

                int StarNumber          =   (TbStarNumber.Value.HasValue) ? (int)TbStarNumber.Value:1;
				CrmRatingCampaignID     =   ( string.IsNullOrEmpty(CbRatingCampaign.SelectedValue) ) ? Guid.Empty: new Guid(CbRatingCampaign.SelectedValue);
                RatingEntityName        =   CbEntityName.SelectedValue;
                if (RatingTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update CrmRatingCampaignID: {0}, RatingEntityName: {1}.",
                        CrmRatingCampaignID,RatingEntityName),RatingTrace.Sw.Info);
                CrmRatingDB.UpdateRatingGlobalized(ModuleID,TbTitleRating.Text,TbDescriptionRating.Text,RatingEntityName,StarNumber,CrmRatingCampaignID);
                if ( useCrm || CrmRatingID != Guid.Empty ) // We must keep using Crm if started and flag changed after
                {
                    if ( createRating )
                        dnr             =   new ADNDynRating(Wrapper,TbTitleRating.Text);
                    dnr.description     =   TbDescriptionRating.Text;
                    dnr.starnumber      =   StarNumber;
                    dnr.assocentity     =   RatingEntityName;
                    dnr.campaignid      =   CrmRatingCampaignID;
                    dnr.addonniceid     =   ModuleID;
                    if ( createRating )
                    {
                            CrmRatingID =   dnr.Create();
                            CrmRatingDB.UpdateRatingCrmID(ModuleID,CrmRatingID);
                    }
                    else    dnr.Update();
                }

                ADNDynRatingDetail dnrd =   null;

                // Get the eventual associated CRM entity
                if ( CrmDetailID != Guid.Empty )
                {
                    dnrd                =   DynEntity.GetFromCache<ADNDynRatingDetail>(CrmStr.ratingdetail,Wrapper,ResetWrapper,CrmDetailID);
                    if ( !dnrd.Retrieved )
                    {
                        string msg      =   string.Format(CultureInfo.InvariantCulture,"[E]CrmRatingsEdit CrmRatingsEdit_Update not found CrmDetailID : {0}",CrmDetailID);
				        if (RatingTrace.Sw.TraceError)
                            Trace2.WriteLine( msg,RatingTrace.Sw.Info);
				        throw new Exception(msg);
                    }
                }
                bool createDetail       =   (dnrd == null);

                if (RatingTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update CrmDetailID: {0}, createDetail: {1}.",
                        CrmDetailID,createDetail),RatingTrace.Sw.Info);

                int itemOrder           =   (TbOrder.Value.HasValue) ? (int)TbOrder.Value:0;
				CrmDetailCampaignID     =   ( string.IsNullOrEmpty(CbDetailCampaign.SelectedValue) ) ? Guid.Empty: new Guid(CbDetailCampaign.SelectedValue);
                Guid CrmImageID         =   Guid.Empty; // To change in a version for Imagine CRM
                
                Guid CrmItemID          =   ( string.IsNullOrEmpty(CbAssocEntity.SelectedValue) ) ? Guid.Empty: new Guid(CbAssocEntity.SelectedValue);

                if (RatingTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update CrmDetailCampaignID: {0}, CrmItemID: {1}, createItem: {2}, Title: {3}.",
                        new object[]{CrmDetailCampaignID,CrmItemID,createItem,TbTitleRatingDetail.Text}),RatingTrace.Sw.Info);

                if ( createItem ) 
				{
                    if (RatingTrace.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update DesktopText.TextContent: '{0}'.",DesktopText.TextContent),RatingTrace.Sw.Info);
					// Add the CrmRating within the CrmRatings table
					ItemID              =   CrmRatingDB.AddRatingDetail(ModuleID,TbTitleRatingDetail.Text,DesktopText.TextContent,CrmItemID,CrmDetailCampaignID,CrmImageID,itemOrder);
                    if (RatingTrace.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update ItemID: {0}.",ItemID),RatingTrace.Sw.Info);
                    if ( useCrm ) 
                    {
                        dnrd                =   new ADNDynRatingDetail(Wrapper,TbTitleRatingDetail.Text);
                        dnrd.description    =   DesktopText.TextContent;
                        if ( CrmDetailCampaignID != Guid.Empty )
                            dnrd.campaignid =   CrmDetailCampaignID;
                        if ( CrmItemID != Guid.Empty )
                            switch(RatingEntityName)
                            {
                                case CrmStr.product:
                                dnrd.productid  =   CrmItemID;
                                break;
                                case CrmStr.account:
                                dnrd.accountid  =   CrmItemID;
                                break;
                                case CrmStr.contact:
                                dnrd.contactid  =   CrmItemID;
                                break;
                                case CrmStr.lead:
                                dnrd.leadid     =   CrmItemID;
                                break;
                            }
                        dnrd.addonniceid    =   ItemID;
                        dnrd.ratingid       =   CrmRatingID;
                        CrmDetailID         =   dnrd.Create();
                        if (RatingTrace.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update dnrd.ratingid: {0}.",dnrd.ratingid),RatingTrace.Sw.Info);
                        CrmRatingDB.UpdateRatingDetailCrmID(ItemID,CrmDetailID);
                    }
				}
				else 
				{
					// Update the CrmRating within the CrmRatings table
					CrmRatingDB.UpdateRatingDetailGlobalized(ItemID,TbTitleRatingDetail.Text,DesktopText.TextContent,CrmItemID,CrmDetailCampaignID,CrmImageID,itemOrder);
                    if (RatingTrace.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit CrmRatingsEdit_Update ratingdetailid: {0}, ratingid: {1}.",dnrd.ratingdetailid,dnrd.ratingid),RatingTrace.Sw.Info);
                    dnrd.title          =   TbTitleRatingDetail.Text;
                    dnrd.description    =   DesktopText.TextContent;
                    if ( CrmDetailCampaignID != Guid.Empty )
                        dnrd.campaignid     =   CrmDetailCampaignID; // could necessitate a value with isnull required
                    if ( CrmItemID != Guid.Empty )
                        switch(RatingEntityName)
                        {
                            case CrmStr.product:
                            dnrd.productid  =   CrmItemID;
                            break;
                            case CrmStr.account:
                            dnrd.accountid  =   CrmItemID;
                            break;
                            case CrmStr.contact:
                            dnrd.contactid  =   CrmItemID;
                            break;
                            case CrmStr.lead:
                            dnrd.leadid     =   CrmItemID;
                            break;
                        }
                    dnrd.Update();
				}
			}
			catch(Exception ex )
			{
				if (RatingTrace.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CrmRatingsEdit OnUpdate ex : {0}",ex),RatingTrace.Sw.Info);
				throw;
			}
            if (RatingTrace.Sw.TraceVerbose)
                Trace2.WriteLine("[V]CrmRatingsEdit CrmRatingsEdit_Update END.",RatingTrace.Sw.Info);
        }

        /// <summary>
        /// The DeleteBtn_Click event handler on this Page is used to delete an
        /// an CrmRating.  It  uses the AddonNice.CrmRatingsDB()
        /// data component to encapsulate all data functionality.
        /// </summary>
        void CrmRatingsEdit_Delete(object sender,AddonNice.UI.ADNPageEventArgs e)
        {
			// Only attempt to delete the item if it is an existing item
            if ( !isNewItem ) 
            {
				try
				{
					CrmRatingDB.DeleteRatingDetailGlobalized(ItemID);
				}
				catch(Exception ex )
				{
					if (RatingTrace.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CrmRatingsEdit OnDelete ex : {0}",ex),RatingTrace.Sw.Info);
                    throw;
				}
            }
        }

        void CbEntityName_SelectedIndexChanged(object o,RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RatingEntityName    =   e.Value;
        }
        
		#region Std overrides

        /// <summary>
        /// Here we create the command buttons and add them to the PlaceHolders prepared
        /// </summary>
        override protected void OnPreInit(EventArgs e)
        {
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]CrmRatingsEdit OnPreInit Begin",PageTraceSwitch.Sw.Info);
            if ( isNewItem ) 
                PageHasDeleteBtn    =   false;
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmRatingsEdit OnPreInit PageHasDeleteBtn: {0}.",PageHasDeleteBtn),PageTraceSwitch.Sw.Info);
            base.OnPreInit(e);
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]CrmRatingsEdit OnPreInit End",PageTraceSwitch.Sw.Info);
        }

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent() 
        {    
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]CrmRatingsEdit InitializeComponent Begin",PageTraceSwitch.Sw.Info);
			Load                                +=  new System.EventHandler(this.Page_Load);
            Update                              +=  new PageEventHandler(CrmRatingsEdit_Update);
            Delete                              +=  new PageEventHandler(CrmRatingsEdit_Delete);
            CbEntityName.AutoPostBack           =   true;
            CbEntityName.SelectedIndexChanged   +=  new RadComboBoxSelectedIndexChangedEventHandler(CbEntityName_SelectedIndexChanged);
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]CrmRatingsEdit InitializeComponent End",PageTraceSwitch.Sw.Info);
		}

		protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get	{ return PlaceHolderButtons; }
			set {}
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get	{ return PlaceHolderButtons2; }
			set {}
		}

		#endregion
    }
}

